package com.example.smart_community.mapper;

import com.example.smart_community.po.Role;
import com.example.smart_community.po.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from user ")
    @Results({
            @Result(property = "uId",column = "u_id"),
            @Result(property = "roleId",column = "role_id"),
            @Result(
                    property ="role" ,column ="role_id",
                    javaType= Role.class,
                    one=@One(select ="com.example.smart_community.mapper.RoleMapper.selectById")
            )
    })
    List<User> selectAll();
    @Select("select * from user where u_id=#{uId}")
    @Results({
            @Result(property = "uId",column = "u_id"),
            @Result(property = "roleId",column = "role_id"),
            @Result(
                    property ="role" ,column ="role_id",
                    javaType= Role.class,
                    one=@One(select ="com.example.smart_community.mapper.RoleMapper.selectById")
            )
    })
    User selectById(Integer uId);
    @Select("select * from user where username=#{username}")
    @Results({
            @Result(property = "uId",column = "u_id"),
            @Result(property = "roleId",column = "role_id"),
            @Result(
                    property ="role" ,column ="role_id",
                    javaType= Role.class,
                    one=@One(select ="com.example.smart_community.mapper.RoleMapper.selectById")
            )
    })
    User selectByName(String username);
    //注册
    @Insert("INSERT `user` (username,userpass) VALUES(#{username},#{userpass})")
    @Results({
            @Result(property = "uId", column = "u_id")
    })
    int insertUser(@Param("username") String username,@Param("userpass") String userpass,@Param("suserpass")String suserpass);
    //修改密码
    @Update("UPDATE user set userpass=#{newpass} where username=#{username}")
    @Results({
            @Result(property = "uId", column = "u_id"),
            @Result(property = "roleId", column = "role_id")
    })
    int updateUser(@Param("username")String username,@Param("userpass") String userpass,@Param("newpass") String newpass);
    //注销
    @Delete("DELETE from user where username=#{username}")
    @Results({
            @Result(property = "uId", column = "u_id")
    })
    int deleteUser(@Param("username")String username,@Param("userpass") String userpass);
}
